package com.dabai.uitools.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.view.View;

import androidx.annotation.RequiresApi;

/**
 * Android主题 辅助工具类
 */
public class ThemeUtils extends Activity {

    /**
     * 自动设置 暗色/亮色 状态栏
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void setAutoUiModeStatusBar(Activity activity, Context context){
        setAndroidNativeLightStatusBar(activity, getDarkModeStatus(context));
    }
    /**
     * 设置主题模式
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) { //暗黑 设置状态栏为白色
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        } else {//设置状态栏为黑色
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }


    /**
     * 获取当前主题模式
     *
     * @param context 上下文
     * @return boolean
     */
    public static boolean getDarkModeStatus(Context context) {
        int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        return mode == Configuration.UI_MODE_NIGHT_YES;
    }


}
